﻿const quoteContent = document.getElementById('quote-container');
const quoteText = document.getElementById('quote');
const authorText = document.getElementById('author');
const twitterBtn = document.getElementById('twitter');
const newQuoteBtn = document.getElementById('new-quote');
const loader = document.getElementById('loader');




// Show Loading
function  loading(){
    loader.hidden = false;
    quoteContent.hidden = true;
}

// Hide Loading
function complete(){
    quoteContent.hidden = false;
    loader.hidden = true;
}

let apiQuotes = [];

// Show New Quote
function newQuote(){
    // Pick a random quote from apiQuotes array
    let randomQuote = Math.floor(Math.random() * apiQuotes.length);
    console.log(apiQuotes[randomQuote]);
}

// Get Quotes From API
async function getQuotes() {
    const aipUrl ='https://type.fit/api/quotes';
    try{
        // const response = await fetch(aipUrl);
        // apiQuotes = await response.json();
        // console.log(apiQuotes);
        for(let i = 0;i < 10;i++){
            let objQuote = {
                text:'What is the quote---' + i,
                author:'Sherlock Moriarty---' + i,
            }
            apiQuotes.push(objQuote);
        }
        newQuote();
    }catch(error){
        // Catch Error here
    }
}

function getLocalQuote(){
    loading();
    // Pick a random quote from localQuotes array
    let randomQuote = Math.floor(Math.random() * localQuotes.length);
    const quote = localQuotes[randomQuote];
    // Check if Author field is blank and replace it with 'unknown'
    if(!quote.author){
        authorText.textContent = 'Unknown';
    }else{
        authorText.textContent = quote.author;
    }
    // Check Quote length to determine styling
    if(quote.text.length > 50){
        quoteText.classList.add('long-quote');
    }else{
        quoteText.classList.remove('long-quote');
    }
    // Set Quote,Hide Loader
    quoteText.textContent = quote.text;
    complete();
}

// Tweet Quote
function tweetQuote(){

}

// On Load
//getQuotes();
getLocalQuote();

newQuoteBtn.addEventListener('click', getLocalQuote);
